package com.eshopkeeper.core.util;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.nio.channels.FileChannel;

/**
 * FILE工具类
 *
 * @author HuangYL
 */
public class FileUtil {

    /**
     * 上传文件
     *
     * @param filePath 上传文件目录
     * @param fileName 保存的文件名称
     * @param file     文件流类
     * @param request  请求
     * @throws IOException IO异常
     */
    public static void updoadFile(String filePath, String fileName, MultipartFile file,
                                  HttpServletRequest request) throws IOException {
        //如果文件夹不存在，则创建文件夹
        File dirPath = new File(filePath);
        if (!dirPath.exists()) {
            dirPath.mkdir();
        }

        File uploadFile = new File(dirPath + "/" + fileName);
        FileCopyUtils.copy(file.getBytes(), uploadFile);
    }

    /**
     * 复制文件
     *
     * @param localFileName 原文件地址和文件名
     * @param tmpFileName   目标文件地址和文件名
     * @throws IOException
     */
    public static void copyFile(String localFileName, String tmpFilePath, String tmpFileName) throws IOException {
        File localFile = new File(localFileName);
        File tmpFile = new File(tmpFilePath, tmpFileName);
        FileCopyUtils.copy(localFile, tmpFile);
    }

    /**
     * 删除文件
     *
     * @param fileName 文件地址和文件名
     * @throws IOException
     */
    public static void deleteFile(String fileName) throws IOException {
        File localFile = new File(fileName);
        localFile.delete();
    }

    /**
     * 下载文件，是向页面输出流，不返回流
     *
     * @param filePath     文件服务器存储目录
     * @param downloadName 下载文件保存的文件名
     * @param fileName     服务器存储文件名
     * @param request
     * @param response
     * @throws UnsupportedEncodingException
     */
    @SuppressWarnings("static-access")
    public static void downloadFile(String filePath, String downloadName, String fileName, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {

        fileName = URLDecoder.decode(fileName, "utf-8");
        downloadName = URLDecoder.decode(downloadName, "utf-8");
        String path = filePath + fileName;

        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String((downloadName).getBytes("GBK"), "iso8859-1"));
        try {
            //以流的形式下载文件
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();

            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取文件的is
     *
     * @param filePath 文件服务器存储目录
     * @param fileName 服务器存储文件名
     * @param request
     * @param response
     * @throws UnsupportedEncodingException
     */
    @SuppressWarnings("static-access")
    public static InputStream getFileIS(String filePath, String fileName, HttpServletRequest request,
                                        HttpServletResponse response) throws UnsupportedEncodingException {

        fileName = URLDecoder.decode(fileName, "utf-8");
        String path = filePath + fileName;
        InputStream fis = null;
        try {
            //以流的形式下载文件
            fis = new BufferedInputStream(new FileInputStream(path));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return fis;
    }

    /**
     * 保存文件
     *
     * @param datas    文件数据
     * @param pathName 文件路径
     */
    public static void saveFile(byte[] datas, String pathName) {
        File file = new File(pathName);
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(file);
            fos.write(datas);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取文件数据
     *
     * @param pathName 文件路径
     * @return
     */
    public static byte[] getFile(String pathName) {
        File file = new File(pathName);
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;

    }

    /**
     * 计算文件大小文件大小
     *
     * @param filePath 文件路径例如：E:\\imgData\\afr\\9211496189393485.jpg
     * @return 文件大小 Kb
     */
    public static long GetFileSize(String filePath) {
        long fileSize = 0L;
        FileChannel fc = null;
        try {
            File f = new File(filePath);
            if (f.exists() && f.isFile()) {
                FileInputStream fis = new FileInputStream(f);
                fc = fis.getChannel();
                fileSize = fc.size() / 1024;
                //logger.info(fileSize);
            } else {
                //logger.info("file doesn't exist or is not a file");
            }
        } catch (FileNotFoundException e) {
            //logger.error(e);
        } catch (IOException e) {
            //logger.error(e);
        } finally {
            if (null != fc) {
                try {
                    fc.close();
                } catch (IOException e) {
                    //logger.error(e);
                }
            }
        }

        return fileSize;
    }

    /**
     * getImage    根据图片的路径获取图片给前台
     *
     * @param response
     * @param request
     * @param filePath
     * @throws Exception
     * @author HuangYL
     * @time 2017年11月24日上午10:41:37
     */
    public void getImage(HttpServletResponse response, HttpServletRequest request, String filePath)
            throws Exception {

        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(response.getOutputStream());

            byte[] data = getFile(filePath);
            bos.write(data);
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                bos.close();
            }
        }
    }
}